<template>
	<el-dialog title="租户审核" v-model="dialogVisible" :width="600" :close-on-click-modal="false" @close="closed">
		<el-form ref="form" :model="form" :rules="rules" label-width="100px">
			<el-form-item label="租户信息">
				<div style="background: #f5f7fa; padding: 15px; border-radius: 4px;">
					<p><strong>公司名称：</strong>{{tenantInfo.company_name || '未设置'}}</p>
					<p><strong>联系人：</strong>{{tenantInfo.contact_name || '未设置'}}</p>
					<p><strong>联系电话：</strong>{{tenantInfo.contact_phone || '未设置'}}</p>
					<p><strong>联系邮箱：</strong>{{tenantInfo.contact_email || '未设置'}}</p>
					<p><strong>租户编码：</strong>{{tenantInfo.tenant_code}}</p>
				</div>
			</el-form-item>
			<el-form-item label="审核结果" prop="status">
				<el-radio-group v-model="form.status">
					<el-radio label="1">通过</el-radio>
					<el-radio label="0">拒绝</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="审核备注" prop="remark">
				<el-input 
					v-model="form.remark" 
					type="textarea" 
					:rows="4" 
					placeholder="请输入审核备注"
					:maxlength="200"
					show-word-limit>
				</el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="closed">取消</el-button>
			<el-button type="primary" @click="submit" :loading="submitLoading">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		name: 'tenantAudit',
		data() {
			return {
				dialogVisible: false,
				submitLoading: false,
				tenantInfo: {},
				form: {
					tenant_id: null,
					status: '1',
					remark: ''
				},
				rules: {
					status: [
						{ required: true, message: '请选择审核结果', trigger: 'change' }
					],
					remark: [
						{ required: true, message: '请输入审核备注', trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			open() {
				this.dialogVisible = true
				this.resetForm()
				return this
			},
			setData(data) {
				this.tenantInfo = data
				this.form.tenant_id = data.id.toString()
				return this
			},
			resetForm() {
				this.form = {
					tenant_id: null,
					status: '1',
					remark: ''
				}
				this.$nextTick(() => {
					this.$refs.form.clearValidate()
				})
			},
			submit() {
				this.$refs.form.validate(async (valid) => {
					if (valid) {
						this.submitLoading = true
						try {
							const res = await this.$API.system.tenant.audit.post(this.form)
							if (res.code === 200) {
								this.$message.success('审核成功')
								this.$emit('success', res.data, 'audit')
								this.closed()
							} else {
								this.$alert(res.message, '提示', { type: 'error' })
							}
						} catch (error) {
							this.$alert('操作失败', '提示', { type: 'error' })
						} finally {
							this.submitLoading = false
						}
					}
				})
			},
			closed() {
				this.dialogVisible = false
				this.$emit('closed')
			}
		}
	}
</script>

<style>

</style>

